import axios from 'axios';
import Qs from 'qs';
import {DEPUTY, BAIDU} from './api';
import {error} from './Notification';//引入Notification通知组件
const userinfo = {
  token: 'P00842'
};//解密,获取用户登录信息
axios.defaults.baseURL = '';//配置axios的默认URL
axios.defaults.timeout = 600000;//请求超时时间
axios.defaults.dataType = 'JSON';
axios.defaults.withCredentials = true;//配置允许跨域携带cookie
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';//设置post默认请求头
// 请求时的拦截
axios.interceptors.request.use(
  config => {// 发送请求之前做一些处理
    if (userinfo) {//设置公共的自定义请求头
      config.headers.token = userinfo.token;
    }
    return config;
  },
  error => {// 当请求异常时做一些处理
    error('网络似乎开小差,请稍后再试!');
    return Promise.reject(error);
  }
);
/**
 *响应时拦截 返回响应时做一些处理
 */
axios.interceptors.response.use(
  response => {
    let data;
    if (response.data === undefined) {
      // IE9时response.data是undefined，因此需要使用response.request.responseText(Stringify后的字符串)
      data = response.request.responseText
    }
    else {
      data = response.data;
      if (data.code !== 200 || response.status !== 200) error(data.msg);
    }
    return data;
  },
  err => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = '请求错误';
          break;

        case 401:
          err.message = '未授权，请登录';
          break;

        case 403:
          err.message = '拒绝访问';
          break;

        case 404:
          err.message = `请求地址出错: ${err.response.config.url}`;
          break;

        case 408:
          err.message = '请求超时';
          break;

        case 500:
          err.message = '服务器内部错误';
          break;

        case 501:
          err.message = '服务未实现';
          break;

        case 502:
          err.message = '网关错误';
          break;

        case 503:
          err.message = '服务不可用';
          break;

        case 504:
          err.message = '网关超时';
          break;

        case 505:
          err.message = 'HTTP版本不受支持';
          break;

        default:
          err.message = err.response.message;
      }
    }
    error(err.message);
    return Promise.reject(err);
  }
);
let root;

/**
 * get方法，对应get请求,url接口地址,params请求的参数,header携带的请求头,type当代理多域名时,判断该使用哪个域名
 */
export function getRequest(url, params, type, headers) {
  if (type === DEPUTY) {
    root = process.env.DEPUTY_ENV
  }
  else if (type === BAIDU) {
    root = process.env.BAI_DU
  }
  else {
    root = process.env.MAIN_ENV
  }
  if (!params) params = {};
  if (!headers) headers = {};
  let path = root + url;
  return new Promise((resolve, reject) => {
    axios.get(path, {params,headers})
    .then(res => {
      resolve(res);
    }).catch(err => {
      reject(err);
    })
  });
}

/**
 * post方法，对应post请求,url接口地址,params请求的参数,header携带的请求头,type当代理多域名时,判断该使用哪个域名
 */
export function postRequest(url, params, type, headers) {
  if (type === DEPUTY) {
    root = process.env.DEPUTY_ENV
  }
  else if (type === BAIDU) {
    root = process.env.BAI_DU
  }
  else {
    root = process.env.MAIN_ENV
  }
  if (!params) params = {};
  if (!headers) headers = {};
  let path = root + url;
  return new Promise((resolve, reject) => {
    axios.post(path, Qs.stringify(params), {headers})
      .then(res => {
        resolve(res);
      })
      .catch(err => {
        reject(err);
      })
  });
}
